home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / smallt~1 / smallt~1.zoo / SequenceableCollection.st < prev    next >
Encoding:
Text File  |  1990-05-26  |  8.4 KB  |  296 lines

  1. "======================================================================
  2. |
  3. |   SequenceableCollection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real method categories.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. Collection variableSubclass: #SequenceableCollection
  40.        instanceVariableNames: ''
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil.
  44.  
  45. SequenceableCollection comment: 
  46. 'My instances represent collections of objects that are ordered.  I provide
  47. some access and manipulation methods.' !
  48.  
  49.  
  50. !SequenceableCollection methodsFor: 'basic'!
  51.  
  52. atAll: aCollection put: anObject
  53.     aCollection do: [ :index | self at: index put: anObject ]
  54. !
  55.  
  56. atAllPut: anObject
  57.     1 to: self size do: [ :index | self at: index put: anObject ]
  58. !
  59.  
  60. first
  61.     ^self at: 1
  62. !
  63.  
  64. last
  65.     self size < 1 ifTrue: [ ^self error: 'last not defined with no elements' ].
  66.     ^self at: self size
  67. !
  68.  
  69. indexOf: anElement ifAbsent: exceptionBlock
  70.     1 to: self size do: [ :index | (self at: index) = anElement
  71.                                      ifTrue: [ ^index ] ].
  72.     ^exceptionBlock value
  73. !
  74.  
  75. indexOf: anElement
  76.     ^self indexOf: anElement ifAbsent: [ ^0 ]
  77. !!
  78.  
  79.  
  80.  
  81. !SequenceableCollection methodsFor: 'nonpublic methods'!
  82.  
  83. matchSubCollection: aSubCollection startingAt: anIndex
  84.     2 to: aSubCollection size do:
  85.         [ :index | (self at: anIndex + index - 1) ~= (aSubCollection at: index)
  86.                    ifTrue: [ ^false ]
  87.     ].
  88.     ^true
  89. !
  90.  
  91. indexOfSubCollection: aSubCollection startingAt: anIndex
  92.                       ifAbsent: exceptionBlock
  93.     | selfSize subSize |
  94.     subSize _ aSubCollection size.
  95.     selfSize _ self size.
  96.     anIndex + subSize <= selfSize ifTrue:
  97.         [ 1 to: subSize do:
  98.             [ :index | (aSubCollection at: index) = (self at: anIndex + index - 1)
  99.                     ifTrue: [ (self matchSubCollection: aSubCollection
  100.                            startingAt: index)
  101.                        ifTrue: [ ^index ]
  102.                         ]
  103.         ] 
  104.         ].
  105.     ^exceptionBlock value
  106. !
  107.  
  108. indexOfSubCollection: aSubCollection startingAt: anIndex
  109.     ^self indexOfSubCollection: aSubCollection startingAt: anIndex
  110.         ifAbsent: [ ^0 ]
  111. !
  112.  
  113. replaceFrom: start to: stop with: replacementCollection startingAt: repStart
  114.     (self == replacementCollection and: [ repStart ~= 1 ])
  115.         ifTrue: [ ^self error: 'replaceFrom:to:with:startingAt: called for 
  116. in-place replacement, but starting index was not 1' ].
  117.     1 to: stop - start + 1 do:
  118.         [ :index |
  119.       self at: (start + index - 1)
  120.                put: (replacementCollection at: (repStart + index - 1)) ]
  121. !
  122.  
  123. replaceFrom: start to: stop with: replacementCollection
  124.     stop - start + 1 ~= replacementCollection size
  125.         ifTrue: [ ^self error: 'replacement range does not equal size of
  126. replacement collection' ].
  127.     self replaceFrom: start to: stop with: replacementCollection startingAt: 1
  128. !!
  129.  
  130.  
  131.  
  132. !SequenceableCollection methodsFor: 'copying SequenceableCollections'!
  133.  
  134. , aSequenceableCollection
  135.     | newCollection |
  136.     newCollection _ self species new: (self size + aSequenceableCollection size).
  137.     newCollection replaceFrom: 1 to: self size with: self.
  138.     newCollection replaceFrom: (self size) + 1
  139.                   to: self size + aSequenceableCollection size
  140.           with: aSequenceableCollection.
  141.     ^newCollection
  142. !
  143.  
  144. copyFrom: start to: stop
  145.     | newCollection len |
  146.     len _ stop - start + 1.
  147.     newCollection _ self species new: len.
  148.     newCollection replaceFrom: 1 to: len with: self startingAt: start.
  149.     ^newCollection
  150. !
  151.  
  152. copyReplaceAll: oldSubCollection with: newSubCollection
  153.     | numOld newCollection sizeDifference newSubSize oldSubSize
  154.       newStart oldStart copySize index |
  155.     numOld _ self countSubCollectionOccurrencesOf: oldSubCollection.
  156.     newSubSize _ newSubCollection size.
  157.     sizeDifference _ newSubSize - oldSubCollection size + 1.
  158.     newCollection _ self species new: (self size - (sizeDifference * numOld)).
  159.     oldStart _ newStart _ 1.
  160.     [ index _ self indexOfSubCollection: oldSubCollection
  161.                    startingAt: oldStart.
  162.       index > 0 ] whileTrue:
  163.         [ copySize _ index - oldStart + 1.
  164.       newCollection replaceFrom: newStart
  165.                     to: newStart + copySize - 1
  166.             with: self
  167.             startingAt: oldStart.
  168.       newStart _ newStart + copySize - 1.
  169.       newCollection replaceFrom: newStart
  170.                     to: newStart + newSubSize - 1
  171.             with newSubCollection.
  172.           oldStart _ oldStart + copySize.
  173.           newStart _ newStart + newSubSize ].
  174.     "Copy the remaining part of self onto the tail of the new collection."
  175.     newCollection replaceFrom: newStart
  176.                  to: newCollection size
  177.          with: self
  178.          startingAt: oldStart.
  179.     ^newCollection
  180. !
  181.  
  182. copyReplaceFrom: start to: stop with: replacementCollection
  183.     | newCollection newSize repSize |
  184.     "### check for bounds "
  185.     repSize _ replacementCollection size.
  186.     newSize _ self size + repSize - (stop - start + 1).
  187.     newCollection _ self species new: newSize.
  188.     newCollection replaceFrom: 1 to: start - 1 with: self startingAt: 1.
  189.     newCollection replaceFrom: start
  190.                   to: start + repSize - 1
  191.           with: replacementCollection.
  192.     newCollection replaceFrom: start + repSize
  193.                   to: newCollection size
  194.           with: self
  195.           startingAt: stop + 1.
  196.     ^newCollection
  197. !
  198.  
  199. copyWith: newElement
  200.     | newCollection len |
  201.     len _ self size + 1.
  202.     newCollection _ self species new: len.
  203.     newCollection replaceFrom: 1 to: self size with: self.
  204.     newCollection at: len put: newElement.
  205.     ^newCollection
  206. !
  207.  
  208. copyWithout: oldElement
  209.     | newCollection numOccurrences i |
  210.     numOccurrences _ 0.
  211.     self do:
  212.         [ :element |
  213.       element = oldElement
  214.         ifTrue: [ numOccurrences _ numOccurrences + 1 ] ].
  215.     newCollection _ self species new: (self size - numOccurrences).
  216.     i _ 1.
  217.     self do:
  218.         [ :element |
  219.       element ~= oldElement
  220.         ifTrue: [ newCollection at: i put: element.
  221.                   i _ i + 1 ]
  222.     ].
  223.     ^newCollection
  224. !!
  225.  
  226.  
  227.  
  228. !SequenceableCollection methodsFor: 'enumerating'!
  229.  
  230. do: aBlock
  231.     "Evaluate aBlock for all elements in the sequenceable collection"
  232.     1 to: self size do:
  233.         [ :i | aBlock value: (self at: i) ]
  234. !
  235.  
  236. findFirst: aBlock
  237.     "Returns the index of the first element of the sequenceable collection
  238.     for which aBlock returns true"
  239.     1 to: self size do:
  240.         [ :i | (aBlock value: (self at: i))
  241.              ifTrue: [ ^i ] ].
  242.     ^0
  243. !
  244.  
  245. findLast: aBlock
  246.     self size to: 1 by: -1 do:
  247.         [ :i | (aBlock value: (self at: i))
  248.              ifTrue: [ ^i ] ].
  249.     ^0
  250. !
  251.  
  252. reverseDo: aBlock
  253.     self size to: 1 by: -1 do:
  254.         [ :i | aBlock value: (self at: i) ]
  255. !
  256.  
  257. with: aSequenceableCollection do: aBlock
  258.     self size = aSequenceableCollection size
  259.         ifFalse:
  260.         [ ^self error: 'sequenceable collections must have same size' ].
  261.     1 to: self size do:
  262.         [ :i | aBlock value: (self at: i)
  263.                   value: (aSequenceableCollection at: i) ]
  264. !!
  265.  
  266.  
  267.  
  268. !SequenceableCollection methodsFor: 'private methods'!
  269.  
  270. countSubCollectionOccurrencesOf: aSubCollection
  271.     | colIndex subColIndex count |
  272.     colIndex _ 1.
  273.     count _ 0.
  274.     [ subColIndex _ self indexOfSubCollection: aSubCollection
  275.                          startingAt: colIndex.
  276.       subColIndex > 0 ] whileTrue:
  277.           [ count _ count + 1.
  278.       colIndex _ colIndex + aSubCollection size ].
  279.     ^count
  280. !
  281.  
  282. grow
  283.     | newCollection |
  284.     newCollection _ self species new: self basicSize + self growSize.
  285.     newCollection replaceFrom: 1 to: self size with: self.
  286.     ^self become: newCollection
  287. !
  288.     
  289. growSize
  290.     ^10                "a randomly chosed number"
  291.  
  292. !!
  293.  
  294.